package com.woniuxy.automotivemanagementsystem.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.woniuxy.automotivemanagementsystem.entity.Role;
import com.woniuxy.automotivemanagementsystem.entity.User;
import com.woniuxy.automotivemanagementsystem.entity.vo.UserVo;
import com.woniuxy.automotivemanagementsystem.service.UserService;
import com.woniuxy.automotivemanagementsystem.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/*
*
* 用户管理页面
* */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    /*
    * 所有用户菜单中的条件查询所有方法
    * */
    @PostMapping("/queryCondition")
    public Result queryCondition(@RequestBody UserVo userFrom){
        return userService.queryCondition(userFrom);
    }
    /*
    * 所有用户菜单中的修改用户信息接口
    * */
    @PutMapping("/update")
    public Result update(@RequestBody User user){
        if(user.getPassword()!=null && !user.getPassword().trim().isEmpty()){
            user.setPassword(DigestUtil.md5Hex(user.getPassword()));
        }else {
            user.setPassword(null);
        }
        userService.saveOrUpdate( user);
        return Result.success(true);
    }
    /*
    * 上传头像接口
    * */
    @PostMapping("/fileUpload")
    public Result fileUpload(MultipartFile file) throws IOException {
        //1.定义保存基础路径
        String basePath = "D://imgtest/";
        //2.定义地址栏访问基础url
        String baseUrl = "http://localhost:80/";

        //3.生成文件名称(文件名唯一)
        String prefix = IdUtil.getSnowflakeNextIdStr();
        //4.截取上传文件后缀
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        //5.拼接文件名
        String filename = prefix + suffix;
        //6.保存文件
        file.transferTo(new File(basePath, filename));
        //7.封装响应url数据
        String imgUrl= baseUrl + filename;
        return Result.success(imgUrl);
    }
    /*
    * 删除单个用户接口
    * */
    @PutMapping("/delete")
    public Result delete(@RequestBody User user){
        if(user.getStatus()==1){
            user.setStatus(0);
            userService.updateById(user);
        }else{
            user.setStatus(1);
            userService.updateById(user);
        }
        return Result.success(true);
    }
    /*
    * 批量删除接口
    * */
    @PutMapping("/updateByUsers")
    public Result updateByUsers(@RequestBody List<User> users){
        for (User user : users) {
            if(user.getStatus()==0){
                user.setStatus(1);
                userService.updateById(user);
            }
        }
        return Result.success(true);
    }
    /*
    * 重新分配角色接口
    * */
    @PutMapping("/updateUserRole/{rIds}")
    public Result updateUserRole(@PathVariable Integer[] rIds, @RequestBody User user){
        if(rIds==null || rIds.length==0){
            return Result.error("角色不能为空");
        }
        else{
            userService.updateUserRole(rIds,user);
        }
        return Result.success(true);
    }
}
